// Page1.cpp : implementation file
//

#include "stdafx.h"
#include "Property5.h"
#include "Page1.h"
#include "mwrf32.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPage1 dialog
    __int16 st; 
	unsigned char _Status[30];
	unsigned char rec_buffer[30];
	unsigned char datatime[7];
	unsigned char dt_temp[2];

CPage1::CPage1(CWnd* pParent /*=NULL*/)
	: CDialog(CPage1::IDD, pParent)
{

	//{{AFX_DATA_INIT(CPage1)
	m_Tlast = 30;
	m_Hwver = _T("");
	m_Proder = _T("");
	m_Port = 0;
	m_Dated = _T("");
	m_Datem = _T("");
	m_Timeh = _T("");
	m_Timem = _T("");
	m_Times = _T("");
	m_Bright = 15;
	m_Datey = _T("");
	m_Dtime = 1;
	//}}AFX_DATA_INIT
}


void CPage1::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPage1)
	DDX_Control(pDX, IDC_COMBO_BAUND, m_BaundCtl);
	DDX_Control(pDX, IDC_STATIC_STATUS, m_Status);
	DDX_Text(pDX, IDC_EDITLAST_TIME, m_Tlast);
	DDX_Text(pDX, IDC_EDIT_HWVER, m_Hwver);
	DDX_Text(pDX, IDC_EDIT_PROSER, m_Proder);
	DDX_Radio(pDX, IDC_RADIOCOM1, m_Port);
	DDX_Text(pDX, IDC_EDITDATED, m_Dated);
	DDX_Text(pDX, IDC_EDITDATEM, m_Datem);
	DDX_Text(pDX, IDC_EDITTIMEH, m_Timeh);
	DDX_Text(pDX, IDC_EDITTIMEM, m_Timem);
	DDX_Text(pDX, IDC_EDITTIMES, m_Times);
	DDX_Text(pDX, IDC_EDIT_BRIGHT, m_Bright);
	DDX_Text(pDX, IDC_EDITDATEY, m_Datey);
	DDX_Radio(pDX, IDC_RADIODtime0, m_Dtime);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPage1, CDialog)
	//{{AFX_MSG_MAP(CPage1)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonBeep)
	ON_BN_CLICKED(IDC_REFREST, OnRefrest)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_RADIODtime0, OnRADIODtime0)
	ON_BN_CLICKED(IDC_RADIODtime1, OnRADIODtime1)
	ON_BN_CLICKED(IDC_RADIOCOM3, OnRadiocom3)
	ON_BN_CLICKED(IDC_RADIOCOM4, OnRadiocom4)
	ON_BN_CLICKED(IDC_RADIOCOM2, OnRadiocom2)
	ON_EN_SETFOCUS(IDC_EDITLAST_TIME, OnSetfocusEditlastTime)
	ON_EN_SETFOCUS(IDC_EDIT_BRIGHT, OnSetfocusEditBright)
	ON_BN_CLICKED(IDC_RADIOCOM1, OnRadiocom1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPage1 message handlers

void CPage1::OnButtonConnect() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
	//__int16 lenth;
	memset(_Status,0,30);
	_Status[30] = 0;
	rec_buffer[30] = 0;
	m_baund = GetBaund();
	rf_exit(icdev);

// 	CString t;
// 	t.Format("%d",m_Port);
// 	MessageBox(t);

	icdev = rf_init(m_Port,m_baund);
	st = rf_get_status(icdev,_Status);
	rf_srd_snr(icdev,16,rec_buffer);
	if((int)icdev<0 || st)
	{ 
		m_Status.SetWindowText("Communicate failed !");
	}
	else
	{
		rf_beep(icdev,10);
		m_Hwver="SC#RF_C_V1.00";
	    m_Proder="SC#RF_C_V1.00";
 //   	m_Hwver="SC###RF_READER0.1";
 //   	m_Proder="SHICHENG-2009.11.10";
//     	m_Hwver.Format("%s",_Status);
// 		m_Proder.Format("%s",rec_buffer);
		m_Status.SetWindowText("Communicate Successful !");

	}
	SetTimer(1,1000,NULL);
	UpdateData(FALSE);
//	rf_exit(icdev);
	
}

long CPage1::GetBaund()
{
	switch(m_BaundCtl.GetCurSel())
	{
	case 0:
		return 9600;
	case 1:
		return 14400;
	case 2:
		return 19200;
	case 3:
		return 28800;
	case 4:
		return 38400;
	case 5:
		return 57600;
	case 6:
		return 115200;
	}
	
    return 9600;

}

void CPage1::OnButtonDisconnect() 
{
	// TODO: Add your control notification handler code here
	__int16 st;
	
	if(icdev == NULL || (int)icdev<0)
        m_Status.SetWindowText("Initialize failed !");
	
	st = rf_exit(icdev);
	if(st)
		m_Status.SetWindowText("Discommunicate failed !");
	else
		m_Status.SetWindowText("Discommunicate Successful !");

	SetTimer(1,1000,NULL);
	UpdateData(FALSE);
}

BOOL CPage1::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class

	
	return CDialog::Create(IDD, pParentWnd);
}

BOOL CPage1::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
// 	// IDM_ABOUTBOX must be in the system command range.
// 	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
// 	ASSERT(IDM_ABOUTBOX < 0xF000);
// 	
// 	CMenu* pSysMenu = GetSystemMenu(FALSE);
// 	if (pSysMenu != NULL)
// 	{
// 		CString strAboutMenu;
// 		strAboutMenu.LoadString(IDS_ABOUTBOX);
// 		if (!strAboutMenu.IsEmpty())
// 		{
// 			pSysMenu->AppendMenu(MF_SEPARATOR);
// 			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
// 		}
// 	}
// 	
// 	// Set the icon for this dialog.  The framework does this automatically
// 	//  when the application's main window is not a dialog
// //	SetIcon(m_hIcon, TRUE);			// Set big icon
// //	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

		UpdateData(TRUE);
		__int8 comn;
		rf_exit(icdev);
		for(comn=0;comn<8;comn++)
		{
			icdev = rf_init(comn,115200);
// 			CString astr;
// 			astr.Format("%d",icdev);
//              MessageBox(astr);
			if((int)icdev>=0)
			{
              break;;
			}
		}

   st = rf_get_status(icdev,_Status);
   rf_srd_snr(icdev,16,rec_buffer);
    if((int)icdev<0 || st)
	{ 
		m_Status.SetWindowText("Communicate failed !");
	}
   else
	{
	//rf_beep(icdev,10);


  //  m_Hwver.Format("%s",_Status);
//	m_Proder.Format("%s",rec_buffer);

	m_Hwver="SC#RF_C_V1.00";
	m_Proder="SC#RF_C_V1.00";
 	}

	CString str; 
	
	CTime tm; tm=CTime::GetCurrentTime();
	m_Datey=tm.Format("%Y");
	m_Datem=tm.Format("%m");
	m_Dated=tm.Format("%d");
	m_Timeh=tm.Format("%H");
	m_Timem=tm.Format("%M");
	m_Times=tm.Format("%S");
    unsigned char datatime[7];
	unsigned char dt_temp[2];
    dt_temp[2] = 0;
    	a_hex(m_Datey.GetBuffer(4),dt_temp,4);	
    datatime[0]=dt_temp[1];
     	a_hex(m_Datem.GetBuffer(2),dt_temp,2);
    datatime[2]=dt_temp[0];
		a_hex(m_Dated.GetBuffer(2),dt_temp,2);
    datatime[3]=dt_temp[0];
		a_hex(m_Timeh.GetBuffer(2),dt_temp,2);
    datatime[4]=dt_temp[0];
		a_hex(m_Timem.GetBuffer(2),dt_temp,2);
    datatime[5]=dt_temp[0];
		a_hex(m_Times.GetBuffer(2),dt_temp,2);
    datatime[6]=dt_temp[0];
    st=rf_settime(icdev,datatime);
    int mode=1; //display time
	st=rf_disp_mode(icdev,mode);

	SetTimer(1,1000,NULL);

	UpdateData(false);


	
	m_Port = 0;
    m_BaundCtl.SetCurSel(6);
	rf_exit(icdev);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPage1::OnButtonBeep() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	__int16 tt = m_Tlast;
	rf_beep(icdev,tt);
	SetTimer(1,1000,NULL);
    UpdateData(FALSE);
	//rf_exit(icdev);
}


void CPage1::OnRefrest() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);

//	__int16 st;
     int bright=m_Bright;
	st=rf_setbright(icdev,bright);

	SetTimer(1,1000,NULL);
	UpdateData(false);
	
}

void CPage1::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CTime tm; tm=CTime::GetCurrentTime(); 
	m_Datey=tm.Format("%Y");
	m_Datem=tm.Format("%m");
	m_Dated=tm.Format("%d");
	m_Timeh=tm.Format("%H");
	m_Timem=tm.Format("%M");
	m_Times=tm.Format("%S");
	UpdateData(false);
	
	CDialog::OnTimer(nIDEvent);
}

void CPage1::OnRADIODtime0() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
		KillTimer(1);
		CTime tm; tm=CTime::GetCurrentTime(); 
		m_Datey=tm.Format("%Y");
		m_Datem=tm.Format("%m");
		m_Dated=tm.Format("%d");
		m_Timeh=tm.Format("%H");
		m_Timem=tm.Format("%M");
	m_Times=tm.Format("%S");
	    dt_temp[2] = 0;
    	a_hex(m_Datey.GetBuffer(4),dt_temp,4);	
    datatime[0]=dt_temp[1];
     	a_hex(m_Datem.GetBuffer(2),dt_temp,2);
    datatime[2]=dt_temp[0];
		a_hex(m_Dated.GetBuffer(2),dt_temp,2);
    datatime[3]=dt_temp[0];

    st=rf_settime(icdev,datatime);
	st=rf_disp_mode(icdev,0);

	UpdateData(false);
	
}

void CPage1::OnRADIODtime1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	//KillTimer(1);
	CTime tm; tm=CTime::GetCurrentTime(); 
	m_Datey=tm.Format("%Y");
	m_Datem=tm.Format("%m");
	m_Dated=tm.Format("%d");
	m_Timeh=tm.Format("%H");
	m_Timem=tm.Format("%M");
	m_Times=tm.Format("%S");
		a_hex(m_Timeh.GetBuffer(2),dt_temp,2);
    datatime[4]=dt_temp[0];
		a_hex(m_Timem.GetBuffer(2),dt_temp,2);
    datatime[5]=dt_temp[0];
		a_hex(m_Times.GetBuffer(2),dt_temp,2);
    datatime[6]=dt_temp[0];

    st=rf_settime(icdev,datatime);
	st=rf_disp_mode(icdev,1);
	SetTimer(1,1000,NULL);
	UpdateData(false);
	
}

void CPage1::OnRadiocom3() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	
}

void CPage1::OnRadiocom4() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	
}

void CPage1::OnRadiocom2() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	
}

void CPage1::OnSetfocusEditlastTime() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	
}

void CPage1::OnSetfocusEditBright() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	
}

void CPage1::OnRadiocom1() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	
}
